package cn.gson.crmdemo.model.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.BxDao;
import cn.gson.crmdemo.model.dao.DeliverDao;
import cn.gson.crmdemo.model.dao.DeliverdetailedDao;
import cn.gson.crmdemo.model.dao.EmpDao;
import cn.gson.crmdemo.model.dao.OutorderDao;
import cn.gson.crmdemo.model.dao.OutorderdetailedDao;
import cn.gson.crmdemo.model.mapper.OrderMapper;
import cn.gson.crmdemo.model.mapper.OutorderMapper;
import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.Reimbursement;
import cn.gson.crmdemo.model.vo.BxVo;
import cn.gson.crmdemo.model.vo.DeliverAddressCustomerOrderVo;
import cn.gson.crmdemo.model.vo.DeliverdetailedProductVo;
import cn.gson.crmdemo.model.vo.OutorderCustomerOrderVo;
import cn.gson.crmdemo.model.vo.OutorderdetailedProductVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class OutorderService {
	@Autowired
	OutorderDao od;
	@Autowired
	OutorderdetailedDao odd;
	@Autowired
	OrderMapper order;
	@Autowired
	DeliverDao dd;//发货
	@Autowired
	DeliverdetailedDao dedao;
	@Autowired
	BxDao bd;
	@Autowired
	EmpDao ed;
	
	/**
	 * 新增/修改出库单
	 * @param emp
	 */
	public void addOutorder(OutorderCustomerOrderVo out) {
		Date date = new Date();//获得系统时间.
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
		String nowTime = sdf.format(date);
		Date time;
		try {
			time = sdf.parse( nowTime );
			out.setOoDate(time);
			od.save(out);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 查询出库单
	 * @param a
	 * @return
	 */
	public JSONArray selectOutorderIdaddDetailed(int a) {
		return order.selectOutorderIdaddDetailed(a);
	}
	
	
	
	/**
	 * 添加出库祥单,并修改库存
	 * @param out
	 */
	public void addOutorderdetailed(OutorderdetailedProductVo out) {
			odd.save(out);
			int num=out.getOodNumber();
			int good=out.getProduct();
			order.updateHouseCk(num,good);
	}
	
	
	/**
	 * 添加发货单
	 * @param deliver
	 */
	public void addDeliver(DeliverAddressCustomerOrderVo deliver) {
			dd.save(deliver);
	}
	
	
	
	/**
	 * 添加发货祥单
	 * @param deliver
	 */
	public void addDeliverdetailed(DeliverdetailedProductVo deliver) {
		dedao.save(deliver);
	}
	
	
	
	/**
	 * 查询最大发货单id
	 * @return
	 */
	public int selectMaxDeliverId() {
		return order.selectMaxDeliverId();
	}
	
	
	
	/**
	 * 查询发货单
	 * @param a
	 * @return
	 */
	public JSONArray selectDeliverIdaddDetailed(int a) {
		return order.selectDeliverIdaddDetailed(a);
	}
	
	
	
	
	/**
	 * 修改订单状态为已发货
	 * @param state
	 */
	public void updateDstateDeliver(int state) {
		order.updateDstateDeliver(state);
	}
	
	
	
	
	
	/**
	 * 添加报销单
	 * @param out
	 * @throws ParseException 
	 */
	public void addReimbursement(BxVo re) throws ParseException {
		
		
		Reimbursement rei=new Reimbursement();
		Emp emp=ed.findById(re.getReimName()).get();
		rei.setReimName(emp);
		rei.setReimMoney(re.getReimMoney());
		Date date = new Date();//获得系统时间.
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
		String nowTime = sdf.format(date);
		Date time = sdf.parse( nowTime );
		
		rei.setReimDate(time);
		rei.setReimRecord(re.getReimRecord());
		rei.setReimState(1);
		bd.save(rei);
	}


	
}
